package com.lzb.yabbs.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lzb.yabbs.R;
import com.lzb.yabbs.dao.Card;
import com.lzb.yabbs.util.TimeManager;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by liuzaibing on 2015/9/7.
 */
public class CardListAdapter extends BaseAdapter {

    private List<Card> mCards=new ArrayList<>();
    private Context mContext;
    private DisplayImageOptions options;


    public CardListAdapter(List<Card> mCards,Context mContext) {
        this.mCards=mCards;
        this.mContext=mContext;
        options=new DisplayImageOptions.Builder().showImageOnLoading(null)
                .showImageOnFail(null)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();
    }

    public void addCards(List<Card> cards){
        mCards.clear();
        mCards.addAll(cards);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mCards.size();
    }

    @Override
    public Card getItem(int position) {
        return mCards.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView==null){
            viewHolder=new ViewHolder();
            convertView= LayoutInflater.from(mContext).inflate(R.layout.card_item,parent,false);
            viewHolder.titleTv= (TextView) convertView.findViewById(R.id.card_title_tv);
            viewHolder.boardTv= (TextView) convertView.findViewById(R.id.board_tv);
            viewHolder.usernameTv= (TextView) convertView.findViewById(R.id.username_tv);
            viewHolder.timeTv= (TextView) convertView.findViewById(R.id.time_tv);
            viewHolder.avatarIv= (CircleImageView) convertView.findViewById(R.id.avatar_iv);
            viewHolder.commentTv= (TextView) convertView.findViewById(R.id.comment_num_tv);
            convertView.setTag(viewHolder);
        }else {
            viewHolder= (ViewHolder) convertView.getTag();
        }
        viewHolder.timeTv.setText(TimeManager.getTimeToNow(getItem(position).getPost_time()));
        viewHolder.titleTv.setText(getItem(position).getTitle());
        viewHolder.boardTv.setText(getItem(position).getBoard());
        viewHolder.usernameTv.setText(getItem(position).getUsername());
        viewHolder.commentTv.setText(getItem(position).getComment_num()+"");
        ImageLoader.getInstance().displayImage(getItem(position).getAvatar(),viewHolder.avatarIv,options);
        return convertView;
    }

    class ViewHolder{
        CircleImageView avatarIv;
        TextView titleTv;
        TextView boardTv;
        TextView usernameTv;
        TextView timeTv;
        TextView commentTv;
    }
}
